//
//  NewFeatureViewController.swift
//  weibo
//  Created by sanya on 15/6/26.
//  Copyright © 2015年 sanya. All rights reserved.
//

import UIKit
import FFAutoLayout

private let reuseIdentifier = "Cell"

class NewFeatureViewController: UICollectionViewController {

    // 界面布局
    let layout = UICollectionViewFlowLayout()
    init(){
        super.init(collectionViewLayout: layout)
    }
    
    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // 注册可重用cell
        self.collectionView!.registerClass(NewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        // 设置布局
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.itemSize = view.bounds.size
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        // 设置分页
        collectionView?.pagingEnabled = true
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
        
    }
    /// 图片总数
    let imageCount = 4
    // collectionView代理方法
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! NewFeatureCell
        cell.imageIndex = indexPath.item

        return cell
    }
    ///  collectionView 停止滚动的动画方法
    ///
    ///  :param: collectionView collectionView
    ///  :param: cell           之前显示的 cell
    ///  :param: indexPath      之前显示的 indexPath
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

        let path = collectionView.indexPathsForVisibleItems().last!
//        print(path.item)
        if path.item == imageCount - 1{
            // 获取cell 让cell播放动画
            (collectionView.cellForItemAtIndexPath(path) as! NewFeatureCell).showStartBtn()
        }

    }
}
// 自定义cell
class NewFeatureCell:UICollectionViewCell {
   // 图片索引
    var imageIndex:Int = 0{
    
        didSet{
            iconView.image = UIImage(named: "new_feature_\(imageIndex + 1)")
            startButton.hidden = true
        }
    }
    // 显示开始按钮
    func showStartBtn(){
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        startButton.hidden = false
        startButton.userInteractionEnabled = false
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 10, options: UIViewAnimationOptions.CurveEaseIn, animations: { () -> Void in
            // transform回复到没变化之前
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
                print("ok")
                self.startButton.userInteractionEnabled = true
        }
    
    
    }
    
    // 图像
    lazy var iconView:UIImageView = {
        return UIImageView()
    
    }()

    // 按钮
    lazy var startButton:UIButton = {
        let btn = UIButton()
        
        btn.frame = CGRectMake(0, 0, 105, 36)
        btn.setTitle("欢迎归来", forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        btn.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return btn
    
    }()
    func startButtonClick(){
        NSNotificationCenter.defaultCenter().postNotificationName(SYSwitchRootVCNotification, object: true)
    
    }
    // 在init方法中添加控件、设置自动布局
    override init(frame: CGRect) {
        super.init(frame: frame)
        addSubview(iconView)
        addSubview(startButton)
        // 自动布局- 一定要在创建完控件就加
        startButton.ff_AlignInner(ff_AlignType.BottomCenter, referView: self, size: nil, offset: CGPoint(x: 0, y: -160))

        
    }

    required init(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // 设置iconView的frame
    override func layoutSubviews() {
        super.layoutSubviews()
        iconView.frame = self.bounds
    }
    
}




